package com.allwees.data.analysis.order.api;

import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.core.common.util.DateUtil;
import com.allwees.data.analysis.base.api.BaseAnalysisApi;
import com.allwees.data.analysis.order.vo.OrderStatisticsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 *
 *
 * @author yangylsky
 * @since 2020-04-07
 *
 */
@Slf4j
@RestController
@RequestMapping("/data/api/analysis/order")
public class OrderApi extends BaseAnalysisApi {
   @GetMapping("/order_create_user")
    public R orderCreateUser(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_CREATE_USER.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_create_num")
    public R orderCreateNum(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_CREATE_NUM.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_create_amt")
    public R orderCreateAmt(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_CREATE_AMT.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_create_detail")
    public R orderCreateDetail(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_CREATE_DETAIL.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_pay_user")
    public R orderPayUser(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_PAY_USER.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_pay_num")
    public R orderPayNum(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_PAY_NUM.txt", dateFrom, dateTo));
    }


    /**
     * 订单数据统计
     * @return
     */
    @GetMapping("/order_statistic_data")
    public R orderStatisticData() {
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        Date dateTo = DateUtil.getPreDay(DateUtil.getDayLast(new Date()));
        //昨日新增订单数
        orderStatisticsVo.setYesterdayOrderPayedNum(query("order/ORDER_PAY_NUM.txt", DateUtil.addHour(dateTo,-24), dateTo));
        //累计订单量
        orderStatisticsVo.setTotalOrderPayedNum(query("order/ORDER_PAY_NUM.txt", DateUtil.getDate(2020,0,1), new Date()));
        //昨日新增GMV($)
        Object object = query("stat_common/YESTERDAY_GMV.txt", DateUtil.addHour(dateTo,-24), dateTo);
        orderStatisticsVo.setYesterdayOrderPayedAmt(object == null? 0 : object);
        //累计GMV($)
        orderStatisticsVo.setTotalOrderPayedAmt(query("stat_common/GMV.txt", DateUtil.getDate(2020,0,1), new Date()));
        return R.ok(orderStatisticsVo);
    }


    /**
     * 昨日新增订单数
     * @return
     */
   /* @GetMapping("/yesterday_order_payed_num")
    public R orderPayNumByYesterday() {
        Date dateTo = DateUtil.getPreDay(DateUtil.getDayLast(new Date()));
        Date dateFrom = DateUtil.addHour(dateTo,-24);
        //log.info("==== yesterday_order_payed_num =====dateFrom:{}",dateFrom);
        //log.info("==== yesterday_order_payed_num =====dateTo:{}",dateTo);
        Date dateFrom = DateUtil.addSeconds(new Date(), -600);
        Date dateTo = new Date();
        return R.ok(query("order/ORDER_PAY_NUM.txt", dateFrom, dateTo));
    }*/

    /**
     * 累计订单量
     * @return
     */
   /* @GetMapping("/total_order_payed_num")
    public R totalOrderPayedNum() {
        Date dateFrom = DateUtil.getDate(2020,0,1);
        return R.ok(query("order/ORDER_PAY_NUM.txt", dateFrom, new Date()));
    }*/

    /**
     * 昨日新增GMV($)
     * @return
     */
   /* @GetMapping("/yesterday_order_payed_amt")
    public R orderPayAmtByYesterday() {
        //Date dateTo = DateUtil.getPreDay(DateUtil.getDayLast(new Date()));
        //Date dateFrom = DateUtil.addHour(dateTo,-24);
        Date dateFrom = DateUtil.addSeconds(new Date(), -600);
        Date dateTo = new Date();
        return R.ok(query("stat_common/YESTERDAY_GMV.txt", dateFrom, dateTo));
    }*/

    /**
     *  累计GMV($)
     * @return
     */
   /* @GetMapping("/total_order_payed_amt")
    public R totalOrderPayedAmt() {
        Date dateFrom = DateUtil.getDate(2020,0,1);
        Date dateTo = new Date();
        return R.ok(query("stat_common/GMV.txt", dateFrom, dateTo));
    }*/



    @GetMapping("/order_pay_amt")
    public R orderPayAmt(Date dateFrom, Date dateTo) {
        //dateFrom = DateUtil.addSeconds(new Date(), -120);
        //dateTo = new Date();
        return R.ok(query("stat_common/GMV.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_pay_detail")
    public R orderPayDetail(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_PAY_DETAIL.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_unpaid_user")
    public R orderUnpaidUser(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_UNPAID_USER.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_unpaid_num")
    public R orderUnpaidNum(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_UNPAID_NUM.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_unpaid_amt")
    public R orderUnpaidAmt(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_UNPAID_AMT.txt", dateFrom, dateTo));
    }

    @GetMapping("/order_unpaid_detail")
    public R orderUnpaidDetail(Date dateFrom, Date dateTo) {
        return R.ok(query("order/ORDER_UNPAID_DETAIL.txt", dateFrom, dateTo));
    }
}
